<template>
    <textarea class="zk-paste" placeholder="选中输入框可直接粘贴QQ截图" @paste="pasteChange"></textarea>
</template>

<script>
import ala from '@/service/ala'
export default {
  props: {
    title: {}
  },
  methods: {
    async pasteChange(ev) {
      let files = ev.clipboardData
      // 如果是 safari 直接return
      if (!(ev.clipboardData && ev.clipboardData.items)) {
        return
      }
      for (let i = 0; i < files.items.length; i++) {
        let item = files.items[i]
        if (item.kind === 'file') {
          let blob = item.getAsFile()
          if (blob.size === 0) {
            return
          }
          let form = new FormData()
          form.append('file', blob)
          let result = await ala.diyHttpPost('Api/StorageFile/upload', form)
          if (result) this.$emit('change', result)
         // var reader = new FileReader()
          // reader.readAsDataURL(blob)
          // reader.onload = async res => {
          //   let form = new FormData()
          //   form.append('file', blob)
          //   // 上传base64
          //   let dataUrl = res.target.result
          //   let result = await ala.diyHttpPost('Api/StorageFile/upload', blob)
          // }
        }
      }
    }
  }
}
</script>

<style lang="scss">
.zk-paste {
  width: 100%;
  height: 100%;
  padding: 20px;
  border: none;
}
</style>